package com.example.limiting.taskmanager2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.List;

public class DetailActivity extends Activity {

    private WorkerAdapter workerAdapter;
    private ListView workerList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        TextView title = (TextView) findViewById(R.id.detail_title);
        TextView startTime = (TextView) findViewById(R.id.detail_start_time);
        TextView endTime = (TextView) findViewById(R.id.detail_end_time);
        TextView type = (TextView) findViewById(R.id.detail_type);
        TextView content = (TextView) findViewById(R.id.detail_content);

        Button finishButton = (Button) findViewById(R.id.finish_button);

        workerList = (ListView) findViewById(R.id.detail_worker_list);
        workerAdapter = new WorkerAdapter(DetailActivity.this,R.layout.worker_list_item);

        Intent intent = getIntent();

        String data = intent.getStringExtra("data");

        JSONTokener jsonParser = new JSONTokener(data);
        try {
            JSONObject body = (JSONObject) jsonParser.nextValue();
            JSONObject jdata = body.getJSONObject("data");
            title.setText(jdata.getString("taskTitle"));
            startTime.setText(jdata.getString("startDate"));
            endTime.setText(jdata.getString("endDate"));
            content.setText(jdata.getString("taskDetail"));
            type.setText(jdata.getString("type"));
            JSONArray jworkers = jdata.getJSONArray("taskUser");
            List<Worker> workers = new ArrayList<Worker>();
            for (int i = 0; i < jworkers.length(); i++) {
                JSONObject jworker = jworkers.getJSONObject(i);
                workers.add(new Worker(jworker.getString("workerName"),jworker.getString("everyDetail")));
            }
            setData(workers);
        } catch (JSONException e) {
            e.printStackTrace();
        }

//        title.setText(intent.getStringExtra("title"));
//        startTime.setText(intent.getStringExtra("start_time"));
//        endTime.setText(intent.getStringExtra("end_time"));
//        type.setText(intent.getStringExtra("type"));
//        content.setText(intent.getStringExtra("content"));
        if (intent.getIntExtra("planet_number",0)==1&&intent.getIntExtra("action_number",1)==0) {
            finishButton.setVisibility(View.VISIBLE);
        }else{
            finishButton.setVisibility(View.GONE);
        }

//        testSetData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void finish_action(View view){
        Toast.makeText(DetailActivity.this,"确认完成！",Toast.LENGTH_SHORT).show();
        this.finish();
    }

    private void setData(List<Worker> workers){
        workerAdapter.addAll(workers);
        workerList.setAdapter(workerAdapter);
        workerList.deferNotifyDataSetChanged();
    }

    private void testSetData(){

        for (int i = 0;i < 20;i++){
            workerAdapter.add(new Worker("test"+i,"test"+i+"应该做什么什么事"));
        }
        workerList.setAdapter(workerAdapter);
//        workerList.setOnItemClickListener(new TaskItemClickListener());
        workerList.deferNotifyDataSetChanged();
    }
}
